﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Components.Share
{
    public partial class CategoryPermission
    {
        public static DataSet SelectBy_CategoryID(int categoryID)
        {
            string spName = "[dbo].p_SHARE_CategoryPermission_SelectBy_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            return db.ExecuteDataSet(dbCommand);
        }

        private static int Insert(SqlTransaction transaction, int roleID, int categoryID, string functionID)
        {
            string spName = "[dbo].p_SHARE_CategoryPermission_Insert";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);

            db.AddInParameter(dbCommand, "@RoleID", SqlDbType.Int, roleID);
            db.AddInParameter(dbCommand, "@FunctionID", SqlDbType.VarChar, functionID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            return db.ExecuteNonQuery(dbCommand, transaction);
        }

        private static int Delete(SqlTransaction transaction, int roleID, int categoryID)
        {
            string spName = "[dbo].p_SHARE_CategoryPermission_DeleteBy_RoleID_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@RoleID", SqlDbType.Int, roleID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            return db.ExecuteNonQuery(dbCommand, transaction);
        }

        private static int Delete(SqlTransaction transaction, string functionID, int categoryID)
        {
            string spName = "[dbo].p_SHARE_CategoryPermission_DeleteBy_FunctionID_CategoryID";
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand)db.GetStoredProcCommand(spName);
            db.AddInParameter(dbCommand, "@FunctionID", SqlDbType.VarChar, functionID);
            db.AddInParameter(dbCommand, "@CategoryID", SqlDbType.Int, categoryID);
            return db.ExecuteNonQuery(dbCommand, transaction);
        }

        public static void UpdatePermissions(string functionID, int categoryID, string[] roles)
        {
            SqlDatabase db = (SqlDatabase)DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                SqlTransaction transaction = connection.BeginTransaction();
                try
                {
                    CategoryPermission.Delete(transaction, functionID, categoryID);
                    foreach (string roleID in roles)
                    {
                        // Administrators Roles.
                        if (roleID != "1")
                        {
                            CategoryPermission.Insert(transaction, Convert.ToInt32(roleID), categoryID, functionID);
                        }
                    }
                    transaction.Commit();
                }
                catch (Exception ex)
                {
                    transaction.Rollback();
                    throw new Exception(ex.Message);
                }
                finally
                {
                    connection.Close();
                }
            }
        }
    }
}